
package com.vanda.db.tag;

/**
 * 比较标签父类<br>
 * 
 * @author 王瑛<br>
 * @version Version 1.00<br>
 */
public abstract class CompareTag extends Tag{
	/** 比较属性 */
	private String property;
	/** 待比较值 */
	private String value;
	
	/** 浮点型比较 */
	private static int DOUBLE_COMPARE = 1;
	/** 整型比较 */
	private static int LONG_COMPARE = 2;
	/** 字符串比较 */
	private static int STRING_COMPARE = 3;
	
	/**
	 * 比较方法，如果返回true,则执行标签，否则跳过标签
	 * @param desired1 条件1
	 * @param desired2 条件2
	 * @return true-条件真，false-条件假
	 */
	protected boolean condition(int desired1, int desired2){
		int type = -1;
		double doubleValue = 0.0;
		long longValue = 0;
		
		Object valueObj = null;
		try{
			valueObj = super.getValue(value);
		}catch(Exception e){
			valueObj = "";
		}
		if ((type < 0) && (value.length() > 0)) {
			try {
				doubleValue = Double.parseDouble(valueObj.toString());
				type = DOUBLE_COMPARE;
			} catch (NumberFormatException e) {
				;
			}
		}
		if ((type < 0) && (value.length() > 0)) {
			try {
				longValue = Long.parseLong(valueObj.toString());
				type = LONG_COMPARE;
			} catch (NumberFormatException e) {
				;
			}
		}
		if (type < 0) {
			type = STRING_COMPARE;
		}

		int result = 0;
		Object variable = null;
		try{
			variable = super.getValue(property);
		}catch(Exception e){
			variable = "";
		}
		if (type == DOUBLE_COMPARE) {
			try {
				double doubleVariable =
					Double.parseDouble(variable.toString());
				if (doubleVariable < doubleValue)
					result = -1;
				else if (doubleVariable > doubleValue)
					result = +1;
			} catch (NumberFormatException e) {
				result = variable.toString().compareTo(valueObj.toString());
			}
		} else if (type == LONG_COMPARE) {
			try {
				long longVariable = Long.parseLong(variable.toString());
				if (longVariable < longValue)
					result = -1;
				else if (longVariable > longValue)
					result = +1;
			} catch (NumberFormatException e) {
				result = variable.toString().compareTo(valueObj.toString());
			}
		} else {
			result = variable.toString().compareTo(valueObj.toString());
		}

		// Normalize the result
		if (result < 0)
			result = -1;
		else if (result > 0)
			result = +1;

		// Return true if the result matches either desired value
		return ((result == desired1) || (result == desired2));
	}
	
	public String getProperty() {
		return property;
	}

	public String getValue() {
		return value;
	}

	public void setProperty(String string) {
		property = string;
	}

	public void setValue(String string) {
		value = string;
	}

}
